<?php
class User_IndexController extends Zend_Controller_Action
{
	public function init()
	{
		parent::init();
		Zend_Layout::startMvc(array('layoutPath' => APPLICATION_PATH . '/modules/user/layouts/scripts/'));
		//$baseurl = Zend_Controller_Front::getInstance()->getBaseUrl();
		//$layout = Zend_Layout::getMvcInstance();
		// Set a layout script path:
		//$layout->setLayoutPath($baseurl.'modules/user/layouts/scripts/');
		// choose a different layout script:
		//$layout->setLayout('layout');
	}

	public function preDispatch(){
		$layout = $this->_helper->layout->setLayout('layout');
	}

	public function indexAction()
	{
		$picture = new User_Model_User();
		$dpic = $picture->listAllUser();
		$this->view->picture = $dpic;
	}

	public function testAction()
	{
		echo '123123';
		$this->view->string = 'big shit';
	}

	public function registerAction()
	{
		//$this->_redirect('user/index/register');
		$layout = $this->_helper->layout->setLayout('register');
	}

	public function insertAction()
	{

		$username = $_GET['username'];
		$password = $_GET['password'];
		$email = $_GET['email'];
		$fullname = $_GET['fullname'];
		$birthday = date('Y-m-d H:m:s', strtotime($_GET['birthday']));
		$address = $_GET['address'];
		$gender = 1;
		$country_id = NULL;
		$city_id = NULL;
		$status = 1;
		$created_time = date('Y-m-d H:m:s');
		$modified_time = NULL;
		$create_user_id = 1;
		$self_introduce = NULL;
		$econtact = NULL;
		$avatar_photo = NULL;

		$data = array(
        'username' => $username,
        'password' => md5($password),
        'email' => $email,
		'full_name' => $fullname,
		'birthday' => $birthday,
		'address' => $address,
		'gender' => $gender,
		'country_id' => $country_id,
		'city_id' => $city_id,
		'status' => $status,
		'created_time' => $created_time,
		'modified_time' => $modified_time,
		'self_introduce' => $self_introduce,
		'econtact' => $econtact,
		'avatar_photo' => $avatar_photo
		);

		$ins = new User_Model_User();
		$last_insert_id = $ins->insertUser($data);
		$this->view->insertComplete = $last_insert_id;
		//$this->view->insertComplete = 'Insert OK';
		$layout = $this->_helper->layout->setLayout('register');
	}

	public function loginAction()
	{
		//die('xxx');
		$db = Zend_Db_Table::getDefaultAdapter();

		$username = $_GET['username'];
		$password = md5($_GET['password']);

		$auth = Zend_Auth::getInstance();
		$authAdapter = new Zend_Auth_Adapter_DbTable($db);

		$authAdapter->setTableName('ipoo_user')
		->setIdentityColumn('username')
		->setCredentialColumn('password');
		$authAdapter->setIdentity($username);
		$authAdapter->setCredential($password);
		$select = $authAdapter->getDbSelect();
		$select->where('status = 1');

		$result = $auth->authenticate($authAdapter);
		$flag = false;
		if ($result->isValid())
		{
				
			$data = $authAdapter->getResultRowObject(null, array('password'));
			$auth->getStorage()->write($data);
			$flag = true;
		}
		if ($flag == true) {
			$this->_redirect('default/index');
		}
	}
}